package mns;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import java.util.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.commons.io.FileUtils;

/**
 * A collection of UserOptions for Modules to pass back and forth with the
 * MNS front end for forms and user options
 * 
 * 
 * @author Dan Moore
 */

@XmlRootElement(namespace = "mns.options")
public class UserOptions {
	
	@XmlElementWrapper(name = "options")
	@XmlElement(name = "option")
	ArrayList<UserOption> options;
	private static String XML_LOC = "tmp.xml";
	
	/**
	 * 
	 * @param XML
	 * @return
	 */
	public UserOptions fromXML(File XML) {
		JAXBContext context;
		Unmarshaller um;
		UserOptions tmp = null;
		try {
			context = JAXBContext.newInstance(UserOptions.class);
			um = context.createUnmarshaller();
			
			tmp = (UserOptions) um.unmarshal(XML);
			
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tmp;
	}
	
	public UserOptions fromXML(String XML) {
		UserOptions tmp = null;
		File file = new File("tmp.xml");
		try {
			FileUtils.writeStringToFile(file, XML);
			tmp = fromXML(file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return tmp;
	}
	
	public File toXMLFile() {
		JAXBContext context;
		Marshaller m;
		File xmlFile = new File(XML_LOC);
		try {
			context = JAXBContext.newInstance(UserOptions.class);
			m = context.createMarshaller();
			
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			
			Writer w = null;
			w = new FileWriter(xmlFile);
			m.marshal(this, w);
			w.close();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return xmlFile;
	}
	
	public String toXMLString() {
		try {
			return readFileAsString(toXMLFile().getAbsolutePath());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean addOption(UserOption opt) {
		if(options == null)
			options = new ArrayList<UserOption>();
		//TODO: How can this fail?
		options.add(opt);
		return true;
	}
	
	public UserOption getOption(String name) {
		if(options == null)
			return null;
		
		for(int i = 0; i < options.size(); i++) {
			if(options.get(i).getName().equals(name))
				return options.get(i);
		}
		return null;
	}
	
	private static String readFileAsString(String filePath) throws IOException {
		StringBuffer fileData = new StringBuffer();
		BufferedReader reader = new BufferedReader(new FileReader(filePath));
		char []buf = new char[1024];
		int numRead = 0;
		while((numRead = reader.read(buf)) != -1) {
			fileData.append(buf, 0, numRead);
		}
		reader.close();
		return fileData.toString();
	}
	
	//- Test Main
	public static void main(String[] args) {
		UserOptions opts = new UserOptions();
		//UserOptions opts2 = opts.fromXML("<options><option><name>zipcode</name><value>30332</value></option></options>");

		opts.addOption(new UserOption("zipcode","30332"));
		System.out.println(opts.toXMLString());
		//opts.addOption(new UserOption("zip", "30318"));
		//opts.addOption(new UserOption("phone","1234567890"));
		/*
		System.out.println(opts.getOption("email").getValue());
		System.out.println(opts.toXMLFile());
		System.out.println(opts2.getOption("email").getValue());*/
		
	}
	
	
}
